package com.dongdong.frog;

import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class FibonacciTest {
    public static void main(String[] args) {

        try {   //防止控制台乱码
            System.setOut(new PrintStream(System.out, true, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //  1,1,2,3,5,8,13,21

        int n = 17; // 我们要求的是第17位斐波那契数
        long fibonacciNumber = fibonacci(n); // 调用方法计算斐波那契数  
        System.out.println("第" + n + "位的斐波那契数是：" + fibonacciNumber);  
    }  
  
    // 计算第n位斐波那契数的方法  
    public static long fibonacci(int n) {  
        if (n <= 0) {  
            throw new IllegalArgumentException("n必须是一个正整数");  
        }  
        if (n == 1) {  
            return 1;
        } else if (n == 2) {  
            return 1;  
        } else {  
            long a = 1;
            long b = 1;  
            long c;  
            for (int i = 3; i <= n; i++) {  
                c = a + b;  
                a = b;  
                b = c;  
            }  
            return b;  
        }  
    }  
}
